package com.foxzzz.binary.search;


public class Solution209 {
    public int minSubArrayLen(int s, int[] nums) {
        if(nums.length==0)
            return 0;
        int start = 0;
        int min = Integer.MAX_VALUE;
        int sum = 0;
        for (int i = 0; i <= nums.length; i++) {
            if (sum >= s) {
                i--;
                min = Math.min(min, i - start);
                sum-=nums[start];
                start++;
            }else {
                if(i==nums.length)
                    break;
                sum+=nums[i];
            }
        }
        return min==Integer.MAX_VALUE?0:min+1;
    }

    public static void main(String[] args) {
        System.out.println(new Solution209().minSubArrayLen(7,new int[]{2,3,1,2,4,3}));
        System.out.println(new Solution209().minSubArrayLen(4,new int[]{1,4,4}));
    }
}
